<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
$container_id =$_GET['container'];
$page_xml = $_GET['page_xml'];

include_once("util/dom.php");

if(!isset($semf_root))
{
	$semf_root = '../';
	// Define that we are returning XML content & not to cache:
	header('Content-Type: text/xml');
	header('Cache-control: no-cache');
}


if(file_exists($page_xml) && strlen(file_get_contents($page_xml)) > 0)
{
	// load the xml
	$doc = new DOMDocument();
	$doc->load($page_xml);
	$doc->validate();
	
	$container = NULL;
	$component = NULL;
	$component_name = NULL;
	
	if(!isset($container_id) || $container_id==NULL || 
							$container_id == 'null') //this come from js  
	{
		$container = $doc->documentElement;
		//$component_name = 'Container'; // this is the root container..
	}
	else
	{
		$container = $doc->getElementById($container_id);
		//$component_name = $container->tagName;
	}
	
	$copy_text = "";
	$ret = generate_component($container, $doc, $semf_root);
	$result = $ret["result"];
	$copy_text = $ret["copy_text"];
	
	$script_text = extract_scripts($result->documentElement, NULL);
	
	if($result != NULL)
	{
		echo $result->saveXML($result->documentElement);
	}
}

function generate_component($container, $doc, $semf_root)
{
	$component = $container->firstChild;
	$result_html = NULL;
	$copy_text = "";
	if ( $component != NULL ) //Not always containers have components..
	{
		$component_name = $component->getAttribute('type');
		
		
		$xslt_file = "./components/${component_name}/editor_gen.xsl";
		
		$component_dir = $semf_root."page_designer/components/${component_name}";
		$result_dir = "../store/other/";
		$copy_text = copy_include_files($result_dir, $component_dir);
		
		$xml_str = $doc->saveXML($component);
		$doc2 = new DOMDocument();
		$doc2 ->loadXML($xml_str);
		
		$xslt = new xsltProcessor;
		$xslt->importStyleSheet(DomDocument::load($xslt_file));
		
		$result_html = $xslt->transformToDoc($doc2);
		
		$child_containers = $component->childNodes;
		
		foreach ( $child_containers as $child_container )
		{
			if ( $child_container->tagName != "container" )
			{
				continue;
			}
			$id = $child_container->getAttribute('id');
			$html_container = $result_html->getElementById($id); //it is inside result..
			$ret = generate_component($child_container, $doc, $semf_root);
			
			$child_html = $ret["result"];
			$copy_text .= $ret["copy_text"];
			
			if ( $child_html != NULL )
			{
			
				appendNode($html_container, $child_html->documentElement, $result_html );
				
			}
		}
		
	}	
	return array("result"=> $result_html, "copy_text"=> $copy_text);
}


?>
